import { MessageHandleManager } from './../../../common/messageHandle/msgHandleMg';
import { FNCManager } from './../../../common/fncManager';
import { ConfigLDCModule } from './../../configLDC/configLDC.module';
import { MaterialModule } from '@angular/material';
import { SharedModule } from './../../../shared.module';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { Component, NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { SystemService } from "../../../service/system.service";

export class OnLineService {
    static Current: OnLineService = new OnLineService();
    private onLine: OnLine;
    Regist(onLine: OnLine) {
        this.onLine = onLine;
    }

    open() {
        if (this.onLine.isHide || this.onLine.showConfig) {
            return;
        }
        let LDCAddress = localStorage.getItem("LDCAddress");
        this.onLine.LDCAddress = LDCAddress;
        this.onLine.isOpen = true;
        this.onLine.isHide = false;
        this.onLine.showConfig = false;
    }

    reset() {
        this.onLine.isOpen = false;
        this.onLine.isHide = false;
        this.onLine.showConfig = false;
    }
}

@Component({
    selector: "div[online]",
    template: `
        <div class="online-mask" *ngIf="isOpen && !showConfig"></div>
        <div fxLayout="column">
            <div class="position-box bgcolor-warn padding-s" fxLayout="column" *ngIf="isOpen && !showConfig">
                <div fxLayout="row">
                    <span class="text-center" fxFlex="100%">本地数据中心连接失败</span>
                </div>
                <div class="v-gap text-s" fxLayout="column">
                    <span>不能连接到本地数据中心将导致店员宝无法进行开台、下单等操作。请按照以下步骤排查问题</span>
                    <span>1、检查收银终端（{{LDCAddress}}）是否开机</span>
                    <span>2、检查网络是否连接正常</span>
                </div>
                <div class="v-gap"></div>
                <button md-button class="bgcolor-white color-lightgray" (click)="configLDC()">重新配置本地数据中心地址</button>
            </div>
            <div class="config-ldc position-box" *ngIf="showConfig">
                <config-ldc [searchMode]="searchMode" (searchSuccess)="searchSuccess($event)"></config-ldc>
            </div>
        </div>
    `,
    styleUrls: ['./online.component.scss']
})
export class OnLine {
    constructor(
        private sysSvr: SystemService
    ) {
        OnLineService.Current.Regist(this);
    }
    isOpen: boolean = false;
    isHide: boolean = false;
    showConfig: boolean = false;
    searchMode: string = "auto";
    LDCAddress;
    close() {
        this.isOpen = false;
        this.isHide = true;
    }
    configLDC() {
        this.showConfig = true;
    }
    async searchSuccess(event) {
        console.log("查找成功***", event);
        await this.FNCConnect();
        this.showConfig = false;
    }
    async FNCConnect() {
        if (this.sysSvr.HostIp) {
            //连接FNS 
            await FNCManager.Current.Connect(this.sysSvr.HostIp);
            FNCManager.Current.FNC.RegistMessageHandler("innerMessage", (message) => {
                console.log("收到消息：", message);
                MessageHandleManager.Current.ReceivedMessage(message);
            });
        }
    }
}

@NgModule({
    imports: [
        CommonModule,
        FormsModule,
        SharedModule,
        ConfigLDCModule
    ],
    exports: [
        OnLine
    ],
    declarations: [
        OnLine
    ],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class OnLineModule { }